//
//  CmtCollectionViewController.m
//  Products_B
//
//  Created by lanou on 16/5/30.
//  Copyright © 2016年 朋学友. All rights reserved.
//

#import "CmtCollectionViewController.h"
#import "CmtNewModel.h"
#import "CmtNewCollectionCell.h"
#import "RequestManager.h"

#import "CmtProductDetailViewController.h"//作品详情

#define kCmtNewUrl @"http://api.izhangchu.com/"

@interface CmtCollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>
@property(nonatomic,strong)NSMutableArray *CmtNewModelArray;

@property(nonatomic,assign)CGPoint OldContentOffset;
@property(nonatomic,assign)CGPoint NewContentOffset;
@property(nonatomic,assign)CGPoint ContentOffset;

@property(nonatomic,assign)int page;
@end

@implementation CmtCollectionViewController

static NSString * const reuseIdentifier = @"CmtNewCell";




- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes
    
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden = YES;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"CmtNewCollectionCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
    
    self.collectionView.scrollEnabled = YES;
    
    
    [self requestCmtDataWithUrlString:kCmtNewUrl];
    
    self.page = 1;
    //    上拉加载更多
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page += 1;
        [self.CmtNewModelArray removeAllObjects];
        [self requestCmtDataWithUrlString:kCmtNewUrl];
    }];
    //  下拉刷新
    self.collectionView.mj_header = [MJRefreshHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self requestCmtDataWithUrlString:kCmtNewUrl];
        
    }];
    
    // Do any additional setup after loading the view.
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    NSLog(@"开始拖拽%lf",scrollView.contentOffset.y);
    self.ContentOffset = scrollView.contentOffset;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //    NSLog(@"滚动%lf",scrollView.contentOffset.y);
    self.NewContentOffset = scrollView.contentOffset;
    
    //    向下滑动
    if (self.NewContentOffset.y > self.ContentOffset.y | self.NewContentOffset.y > self.OldContentOffset.y) {
        NSLog(@"向下滑");
        
        self.block(YES);
        
    }else{
        
        NSLog(@"向上滑");
        self.block(NO);
    }
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSLog(@"开始减速%lf",scrollView.contentOffset.y);
    self.OldContentOffset = scrollView.contentOffset;
}




-(void)requestCmtDataWithUrlString:(NSString *)urlString{

    NSDictionary *parDic = @{@"page":[NSString stringWithFormat:@"%d",self.page],@"methodName":@"ShequList",@"size":@"20",@"version":@"4.40&"};
    
    [RequestManager requestWithUrl:kCmtNewUrl requestType:(RequestTypePOST) parDic:parDic finish:^(NSData *data) {
        NSDictionary *jsDic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
//        NSLog(@"%@",jsDic);
        
        self.CmtNewModelArray = [CmtNewModel setModelWithJSdic:jsDic];
        [self.collectionView reloadData];
        
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
        
    } error:nil];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return _CmtNewModelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    CmtNewCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    if (indexPath.row != 0) {
        cell.model = self.CmtNewModelArray[indexPath.row];
    }
    
    return cell;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    return UIEdgeInsetsMake(0, 0, 0, 0);
}


-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{

    return 0;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(kWidth / 2, kWidth / 2 + 70);
}

//collectview的点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    CmtNewModel *model = self.CmtNewModelArray[indexPath.row];
    
    CmtProductDetailViewController *CmtSDTableVc = [[CmtProductDetailViewController alloc]init];
    CmtSDTableVc.post_id = model._id;
    [self.parentViewController.navigationController pushViewController:CmtSDTableVc animated:YES];
    
}

//视图将要消失-隐藏导航栏
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.parentViewController.navigationController.navigationBarHidden = YES;

}

//试图将要出现-出现导航栏
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.parentViewController.navigationController.navigationBarHidden = NO;
}


#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
